package com.yearner.reviewplan.plan.model;

import apijson.JSON;
import com.yearner.reviewplan.common.util.StringUtils;
import com.yearner.reviewplan.plan.commands.UpdatePlanCommand;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

/**
 * @author pengyg
 * @since 2019/10/12
 */
@Getter
@Setter
public class Plan {
    private PlanId planId = null;
    /**
     * 当前所处阶段
     */
    private int stageIndex;
    /** 标题  */
    private String title;
    /** 详情-富文本  */
    private String detail;
    /** 详情-Markdown  */
    private String detailMd;
    /** 标题--问题  */
    private String question;
    /** 问题列表  */
    private List<Question> questionList;
    /** 上次复习时间  */
    private long lastTime;
    /**  复习周期最多几次  */
    private static final int MIN_CYCLE_LENGTH = 7;
    /** 标签，用 , 分隔 */
    private String tag;
    //--------------- 构造相关 -------------------

    private Plan(PlanId id) {
        this.planId = id;
    }


    protected Plan() {
        this.stageIndex = 0;
    }

    public static Plan create(PlanId id) {
        Plan plan = new Plan(id);
        plan.setStageIndex(0);
        return plan;
    }
    //--------------- 构造相关end -------------------

    /**
     * 更新标题与详情
     *
     * @param command UpdatePlanCommand
     */
    public final void updatePlan(UpdatePlanCommand command) {
        setTitle(command.getTitle());
        setDetail(command.getDetail());
        setDetailMd(command.getDetailMd());
        setQuestion(command.getQuestion());
        setQuestionList(command.getQuestionList());
        setTag(command.getTag() == null ? "[]" : JSON.toJSONString(command.getTag()));
    }

    /**
     * 重新设置阶段数
     *
     * @param stage
     */
    public final void changeStage(int stage) {
        this.stageIndex = Math.min(stage,MIN_CYCLE_LENGTH);
    }

    /**
     * 完成当阶段，进入到下一阶段
     */
    public final void finishThisTime(Integer point) {
        point = (point == null) ? 1 : point;
        // 有可能小于0
        this.stageIndex = Math.max(stageIndex + point, 0);
        // 防止越界
        this.stageIndex = Math.min(this.stageIndex, MIN_CYCLE_LENGTH);
        this.lastTime = System.currentTimeMillis();
    }


}
